import { ProTableColumn } from '@/components/pro-table/type'
import { NButton, NPopconfirm } from 'naive-ui'
import { TaskModel } from '@/api/model/task-model'
import { Ref } from 'vue'
import { userInfo } from '~/types/user'
import { userInfoModel } from '@/api/model/userModel'

type Props = {
  fetchDelTask: (id: number) => void
  handleCheck: (row: TaskModel, type: 'detail' | 'check') => void
  isInAllocate: Ref<boolean>
  isInBorrow: Ref<boolean>
  userInfo: Ref<userInfo<userInfoModel>>
}

export const useTaskProTable = ({
  fetchDelTask,
  handleCheck,
  isInAllocate,
  isInBorrow,
  userInfo
}: Props) => {
  const showTask = (row) => {
    if (row.taskType === '6') {
      return !isAllocateDetail(row)
    } else if (row.taskType === '3') {
      return !isBorrowDetail(row)
    } else {
      return row.taskStatus === '0'
    }
  }

  const showDetail = (row) => {
    if (row.taskType === '6') {
      return isAllocateDetail(row)
    } else if (row.taskType === '3') {
      return isBorrowDetail(row)
    } else {
      return row.taskStatus !== '0'
    }
  }

  // 是否为调拨详情
  const isAllocateDetail = (row) => {
    const isInAllocateType = row.receiveBranchId === userInfo.value.orgId
    return isInAllocateType
      ? row.taskInStatus !== '0'
      : row.taskOutStatus !== '0'
  }

  // 是否为借用详情
  const isBorrowDetail = (row) => {
    const isInBorrowType = row.receiveBranchId === userInfo.value.orgId
    return isInBorrowType ? row.taskInStatus !== '0' : row.taskOutStatus !== '0'
  }

  const columns = computed<ProTableColumn<TaskModel>[]>(() => {
    const list = [
      {
        title: '申请名称',
        key: 'taskName',
        query: {
          type: 'input'
        }
      },
      {
        type: 'dict',
        title: '申请类型',
        key: 'taskType',
        dictOptions: useDictData('TASK_TYPE').value,
        query: {
          type: 'select',
          dictOptions: useDictData('TASK_TYPE').value
        }
      },
      {
        title: '申请人',
        key: 'receivePerson',
        query: {
          type: 'input'
        }
      },
      {
        title: '申请部门',
        key: 'receiveBranch',
        query: {
          type: 'input'
        }
      },
      {
        title: '申请日期',
        key: 'receiveDate'
      },
      {
        type: 'tag',
        title: '申请状态',
        key: 'taskStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      {
        title: '审核人',
        key: 'taskPerson'
      },
      {
        title: '审核日期',
        key: 'taskDate'
      },
      {
        type: 'tag',
        title: '调入/借入申请状态',
        key: 'taskInStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      {
        type: 'tag',
        title: '调出/借出申请状态',
        key: 'taskOutStatus',
        dictOptions: useDictData('STATUS').value,
        query: {
          type: 'select',
          dictOptions: useDictData('STATUS').value
        }
      },
      {
        title: '操作',
        key: 'operation',
        render(row) {
          return (
            <>
              {showTask(row) ? (
                <NButton
                  type="primary"
                  size="small"
                  onClick={() => handleCheck(row, 'check')}
                  quaternary
                >
                  审核
                </NButton>
              ) : (
                ''
              )}
              {showDetail(row) ? (
                <NButton
                  type="info"
                  size="small"
                  onClick={() => handleCheck(row, 'detail')}
                  quaternary
                >
                  详情
                </NButton>
              ) : (
                ''
              )}
              {/* <NPopconfirm onPositiveClick={() => fetchDelTask(row.id)}>
                {{
                  trigger: () => (
                    <NButton size="small" type="error" quaternary>
                      删除
                    </NButton>
                  ),
                  default: () => '您确定要删除？'
                }}
              </NPopconfirm> */}
            </>
          )
        }
      }
    ]
    return list
  })

  return {
    columns
  }
}
